package com.medata.utils;

import com.medata.dataObject.Templatefields;
import com.medata.exeptions.YunExceptions;
import com.medata.service.TemplatefieldsService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：zb
 * @date ：Created in 2019/5/30 0030 10:26
 * @description：
 */

public class SolrXmlUtil {
    public static String returnXml(TemplatefieldsService templatefieldsService, String dbid) {
        String xml = "<returnfield>";
        String[] dbids = dbid.split("-");
        for (String s:dbids){
            List<Templatefields> templatefieldsList = templatefieldsService.getByDbid(Integer.valueOf(s));
            for (Templatefields templatefields : templatefieldsList) {
                String sxml = " <fieldname desc=\"" + templatefields.getTpFieldDesc() + "\">" + templatefields.getTpFieldName() + "</fieldname>";
                xml = xml + sxml;
            }
        }
        xml = xml + "<fieldname desc=\"更新时间\">CreateDate</fieldname>" + "<fieldname desc=\"是否有文件\">hasobject</fieldname>" + "</returnfield>";
        return xml;
    }

    public static String headXml(String dbid) {
        String xml=null;
        dbid=dbid.replace("-",",");
        String action=dbid.contains(",")?"muldbsearch":"ADVSEARCH";
        xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<parameter><action>"+action+"</action><isred>0</isred>";
        xml = xml + "<dbid>" + dbid + "</dbid>" + "<changeparameter>" + dbid + "</changeparameter>" + "<pagedown>0</pagedown>";
        return xml;
    }

    public static String searchXml(TemplatefieldsService templatefieldsService, Integer dbid, String word, String str, String curpage) {
        List<Templatefields> templatefieldsList = templatefieldsService.getByDbid(dbid);
        Templatefields templatefields = null;
        if (word != null) {
            for (Templatefields templatefields1 : templatefieldsList) {
                if (templatefields1.getTpFieldDesc().equalsIgnoreCase(word)) {
                    templatefields = templatefields1;
                }
            }
        }
        String xml = "<searchfield><advfield><field name=\"";
        if (str == null || str.isEmpty()) {
            str = String.valueOf(dbid);
//            xml=xml+"DBID"+"\" relation=\"and\" ishotkeyword=\"0\" searchkeyword=\""+str+"\">"+str+"</field>"+"</advfield></searchfield><filterfield/><ranfield/><sortfield><fieldname orderby=\"1\" fieldtype=\"string\">CreateDate</fieldname></sortfield>";
            xml = xml + "DBID" + "\" relation=\"and\" ishotkeyword=\"0\" searchkeyword=\"" + str + "\">" + str + "</field>" + "</advfield></searchfield><filterfield/><sortfield><fieldname orderby=\"1\" fieldtype=\"string\">CreateDate</fieldname></sortfield>";
            xml = xml + "<curpage>" + curpage + "</curpage><pagenum>10</pagenum></parameter>";
        } else {
//            xml=xml+templatefields.getTpFieldName()+"\" relation=\"and\" ishotkeyword=\"0\" >"+str+"</field>"+"</advfield></searchfield><filterfield/><ranfield/><sortfield><fieldname orderby=\"1\" fieldtype=\"string\">CreateDate</fieldname></sortfield>";
            xml = xml + templatefields.getTpFieldName() + "\" relation=\"and\" ishotkeyword=\"0\" >" + str + "</field>" + "</advfield></searchfield><filterfield/><sortfield><fieldname orderby=\"1\" fieldtype=\"string\">CreateDate</fieldname></sortfield>";
            xml = xml + "<curpage>" + curpage + "</curpage><pagenum>10</pagenum></parameter>";
        }
        return xml;
    }

    public static String portSearchXml2(TemplatefieldsService templatefieldsService,String dbid, List<String> sFields) {
        StringBuffer sb = new StringBuffer("");
        int x=0;
        if (sFields.size() == 1 && sFields.get(0).equalsIgnoreCase("0")) {
            sb.append("<advfield><field name=\"DBID\" relation=\"and\" >"+dbid+"</field></advfield>");
        } else {
            sb.append("<advfield>");
            int m=0;
            for (int i = 0; i < sFields.size(); i++) {
                String key = sFields.get(i);
                String[] ks = key.split(":");
                if (ks[1].equalsIgnoreCase("0")){
                    m++;
                }
                String sf = ks[0];
                String relation="and";
                if (sf.contains("-")){
                    relation="or";
                    sf=sf.replace("-","");
                }
                if (sf.equalsIgnoreCase("编号")){
                    sb.append("<field name=\"id\" relation=\""+relation+"\" >" + ks[1] + "</field>");
                    x++;
                }else{
                    Templatefields field = templatefieldsService.getByDbidAndTpFieldDesc(Integer.valueOf(dbid), sf);
                    sb.append("<field name=\"" + field.getTpFieldName() + "\" relation=\""+relation+"\" >" + ks[1] + "</field>");
                    x++;
                }
            }
            if (x==0){
                sb.append("<field name=\"DBID\" relation=\"and\" >"+dbid+"</field>");
            }
            if (m==sFields.size()){
                sb=new StringBuffer("<advfield><field name=\"DBID\" relation=\"and\" >"+dbid+"</field>");
            }
            sb.append("</advfield>");
        }
        return sb.toString();
    }

    public static String portSearchXml(String dbid, List<String> sFields, List<String> values) {
        StringBuffer sb = new StringBuffer("");
        int x=0;
        if (sFields.size() == 1 && sFields.get(0).equalsIgnoreCase("0")) {
            if (dbid.contains("-")){
                sb.append("<advfield></advfield>");
            }else{
                sb.append("<advfield><field name=\"DBID\" relation=\"and\" >"+dbid+"</field></advfield>");
            }
            x++;
        } else {
            sb.append("<advfield>");
            for (int i = 0; i < sFields.size(); i++) {
                String key = sFields.get(i);
                String value = values.get(i);
                if (!value.equalsIgnoreCase("0")){
                    String[] split = key.split(":");
//                    StringBuffer str = new StringBuffer("<field name=\"" + split[1] + "\" relation=\"and\" >" + value + "</field>");
                    String[] vs = value.split("->");
                    StringBuffer str = new StringBuffer("");
                    if (vs.length>1){
                        for (int m=0;m<vs.length;m++){
                            str.append("<field name=\"" + split[1] + "\" relation=\"or\" >" + vs[m] + "</field>");
                        }
                    }else {
                        String sstr="<field name=\"" + split[1] + "\" relation=\"and\" >" + value + "</field>";
                        str.append(sstr);
                    }
                    sb.append(str);
                    x++;
                }
            }
            if (x==0){
                sb.append("<field name=\"DBID\" relation=\"and\" >"+dbid+"</field>");
            }
            sb.append("</advfield>");
        }
        return sb.toString();
    }


    public static String portReturnXml(TemplatefieldsService templatefieldsService, String dbid, List<String> rFields) {
        if (rFields.size() == 1 && rFields.get(0).equalsIgnoreCase("0")) {
            return returnXml(templatefieldsService, dbid);
        } else {
            List<Templatefields> templatefields = templatefieldsService.getByDbid(Integer.valueOf(dbid));
            List<Templatefields> collect = templatefields.stream().filter(e -> rFields.contains(e.getTpFieldDesc())).collect(Collectors.toList());
            StringBuffer sb = new StringBuffer("<returnfield>");
            for (Templatefields t:collect){
                sb.append("<fieldname desc=\"" + t.getTpFieldDesc() + "\">" + t.getTpFieldName() + "</fieldname>");
            }
            sb.append("</returnfield>");
            return sb.toString();
        }
    }

    public static String portSortXml(String word) {
        String[] split = word.split(":");
        if (split[1].equalsIgnoreCase("0")) {
            return "<sortfield><fieldname orderby=\"0\" fieldtype=\"string\">" + split[0] + "</fieldname></sortfield>";
        } else {
            return "<sortfield><fieldname orderby=\"1\" fieldtype=\"string\">" + split[0] + "</fieldname></sortfield>";
        }
    }

    public static String portFilterXml(List<String> fFields) {
        if (fFields.size() == 1 && fFields.get(0).equalsIgnoreCase("0")) {
            return "<filterextent/>";
        } else {
            StringBuffer sb = new StringBuffer("<filterextent>");
            for (String s : fFields) {
                String[] split = s.split(":");
                String[] ss = split[2].split("-");
                sb.append("<field name=\"" + split[01] + "\" type=\"double\" minextent=\""+ss[0]+"\" maxextent=\""+ss[1]+"\" left=\"between\" right=\"between\" ></field>");
            }
            sb.append("</filterextent>");
            return sb.toString();
        }
    }
    public static String portFilterXml2(TemplatefieldsService templatefieldsService,Integer dbid,List<String> fFields) {
        if (fFields.size() == 1 && fFields.get(0).equalsIgnoreCase("0")) {
            return "<filterextent/>";
        } else {
            StringBuffer sb = new StringBuffer("<filterextent>");
            for (String s : fFields) {
                String[] split = s.split(":");
                Templatefields tf = templatefieldsService.getByDbidAndTpFieldDesc(dbid, split[0]);
                String[] ss = split[1].split("-");
                sb.append("<field name=\"" + tf.getTpFieldName() + "\" type=\"double\" minextent=\""+ss[0]+"\" maxextent=\""+ss[1]+"\" left=\"between\" right=\"between\" ></field>");
            }
            sb.append("</filterextent>");
            return sb.toString();
        }
    }

    public static String portPsXml(String curpage, String pagesize) {
        return "<curpage>" + curpage + "</curpage><pagenum>" + pagesize + "</pagenum></parameter>";
    }

    public static String portFactXml(List<String> factFields) {
        if (factFields.size() == 1 && factFields.get(0).equalsIgnoreCase("0")) {
            return "<ranfield/>";
        } else {
            StringBuffer sb = new StringBuffer("<ranfield>");
            for (String s : factFields) {
                String[] split = s.split(":");
                if (split.length == 3) {
                    String ts = split[2];
                    String[] tim = ts.split("-");
                    if (tim.length != 2) {
                        throw new YunExceptions(0, "分面时间格式不正确");
                    }
                    sb.append("<field name=\"" + split[1] + "\" type=\"fielddate\" facetMinCount=\"1\" facetLimit=\"20\" datestart=\"" + tim[0] + "\" dateend=\"" + tim[1] + "\" format=\"yyyy\"/>");
                } else {
                    sb.append("<field name=\"" + split[1] + "\" type=\"fieldstring\" facetMinCount=\"1\" facetLimit=\"20\" datestart=\"\" dateend=\"\" />");
                }
            }
            sb.append("</ranfield>");
            return sb.toString();
        }
    }


    public static String portFactXml2(Integer dbid,TemplatefieldsService templatefieldsService,List<String> factFields) {
        if (factFields.size() == 1 && factFields.get(0).equalsIgnoreCase("0")) {
            return "<ranfield/>";
        } else {
            StringBuffer sb = new StringBuffer("<ranfield>");
            for (String s : factFields) {
                String[] split = s.split(":");
                String desc = split[0];
                Templatefields tf = templatefieldsService.getByDbidAndTpFieldDesc(dbid, desc);
                if (split.length ==2) {
                    String ts = split[1];
                    String[] tim = ts.split("-");
                    if (tim.length != 2) {
                        throw new YunExceptions(0, "分面时间格式不正确");
                    }
                    sb.append("<field name=\"" + tf.getTpFieldName() + "\" type=\"fielddate\" facetMinCount=\"1\" facetLimit=\"20\" datestart=\"" + tim[0] + "\" dateend=\"" + tim[1] + "\" format=\"yyyy\"/>");
                } else {
                    sb.append("<field name=\"" + tf.getTpFieldName() + "\" type=\"fieldstring\" facetMinCount=\"1\" facetLimit=\"20\" datestart=\"\" dateend=\"\" />");
                }
            }
            sb.append("</ranfield>");
            return sb.toString();
        }
    }
}
